import he from 'he';

export default class HtmlEntity {

  static Encoder = class Encoder {
    static Name = "HTML Entity <%= UIText.encode %>";

    static Options = {
      useNamedReferences: {type: 'switch', label: 'Named References'},
      decimal: {type: 'switch', label: 'Decimal'},
      encodeEverything: {type: 'switch', label: 'All Chars'},
      escape: {type: 'switch', label: 'Grammar Chars'},
    }

    options = {
      useNamedReferences: false,
      decimal: false,
      encodeEverything: false,
    }

    compute(text) {
      const escape = this.options.escape;
      delete this.options.escape;
      if (escape) {
        return he.escape(text, this.options);
      } else {
        return he.encode(text, this.options);
      }
    }
  }

  static Decoder = class Decoder {
    static Name = "HTML Entity <%= UIText.decode %>";

    static Options = {
      strict: {type: 'switch', label: 'strict'},
    }

    options = {
      strict: false,
    }

    compute(text) {
      return he.decode(text, this.options);
    }
  }
}
